const Base = require('./base');

module.exports = class extends Base {
  async listAction() {
    const pageSize = this.request.query.pageIndex || 10;
    const pageIndex = this.request.query.pageIndex || 1;
    const count = await this.mysql('hanyu_store').count('*');
    const data = await this.mysql('hanyu_store')
      .limit(pageSize).offset((pageIndex - 1) * pageSize);
    for (let i in data) {
      data[i]['create_time'] = saas.datetime(data[i]['create_time']);
    }
    return this.success({
      data: data,
      count: count[0]['count(*)']
    });
  }
  
  async editAction() {
    const model = this.request.body;
    if (saas.isEmpty(model.create_time)){
      model.create_time = Date.now();
    }else{
      delete model.create_time;
    }
    await this.mysql.thenUpdate('hanyu_store', {id: model.id || null}, model);
    return this.success();
  }
  
  async delAction() {
    const id = this.request.body.id;
    await this.mysql('hanyu_user').where({id: id}).del();
    return this.success();
  }
  
}